package com.example.myapplication.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.RadioGroup;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.example.myapplication.R;
import com.example.myapplication.fragment.HealthFragment;
import com.example.myapplication.fragment.HomeFragment;
import com.example.myapplication.fragment.MessageFragment;
import com.example.myapplication.fragment.PersonalFragment;

public class MainActivity extends AppCompatActivity {
    private RadioGroup radioGroup;
    private HomeFragment homeFragment;
    private HealthFragment healthFragment;
    private MessageFragment messageFragment;
    private PersonalFragment personalFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        SharedPreferences sharedPreferences = getSharedPreferences("Chihiro", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();

        boolean firstIn = sharedPreferences.getBoolean("firstIn",true);
        if (firstIn){
            Intent intent = new Intent(MainActivity.this,WelcomeActivity.class);
            startActivity(intent);
            finish();
        }
        initView();
        radioGroup.check(getRadioButtonId(0));
        replaceFragment(homeFragment);
        radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == getRadioButtonId(0)) {
                replaceFragment(healthFragment);
            } else if (checkedId == getRadioButtonId(1)) {
                replaceFragment(healthFragment);
            } else if (checkedId == getRadioButtonId(2)) {
                replaceFragment(messageFragment);
            } else if (checkedId == getRadioButtonId(3)) {
                replaceFragment(personalFragment);
            }
        });

    }

    private int getRadioButtonId(int index) {
        return radioGroup.getChildAt(index).getId();
    }

    private void replaceFragment(Fragment fragment) {
        FragmentTransaction fragmentTransaction;
        fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.frame_layout, fragment).commit();
    }

    private void initView() {
        radioGroup = findViewById(R.id.tab_bar);
        homeFragment = new HomeFragment();
        healthFragment = new HealthFragment();
        messageFragment = new MessageFragment();
        personalFragment = new PersonalFragment();
    }
}